package com.qdxwx.data.service.channel;

import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.mapper.channel.ChannelMapper;
import com.qdxwx.models.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    @Override
    public int insert(Channel channel) {
        channel.setCreateTime(LocalDateTime.now());
        int count = channelMapper.insertChannel(channel);
        if (count > 0) {
            CacheUtils.remove(CacheConstants.CHANNEL_MENUS);
        }
        return count;
    }

    @Override
    public int update(Channel channel) {
        int count = channelMapper.updateChannel(channel);
        if (count > 0) {
            CacheUtils.remove(CacheConstants.CHANNEL_MENUS);
        }
        return count;
    }

    @Override
    public Channel get(int channelId) {
        if (channelId < 1) return null;
        return channelMapper.selectById(channelId);
    }

    @Override
    public Channel getByDir(String channelDir) {
        if (TKString.isEmpty(channelDir)) return null;
        if (!TKString.isEnglish(channelDir)) return null;
        return channelMapper.selectByDir(channelDir);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Channel> getMenus(int companyId) {
        if (companyId < 1) return new ArrayList<>();
        List<Channel> channels = (List<Channel>) CacheUtils.get(CacheConstants.CHANNEL_MENUS + companyId);
        if (channels == null) {
            channels = channelMapper.selectListByCompanyId(companyId);
            CacheUtils.set(CacheConstants.CHANNEL_MENUS + companyId, channels);
        }
        return channels;
    }

    @Override
    public List<Channel> getMenusByModuleId(int moduleId) {
        Channel query = new Channel();
        query.setIsHide(0);
        query.setIsDisabled(0);
        query.setModuleId(moduleId);
        query.setChannelType(1);
        return getList(query, null);
    }

    @Override
    public List<Channel> getList(Channel channel, PageRequest page) {
        return channelMapper.selectList(channel, page);
    }

    @Override
    public long count(Channel channel) {
        return channelMapper.selectCount(channel);
    }

    @Override
    public int delete(int channelId) {
        if (channelId < 1) return 0;
        int count = channelMapper.deleteById(channelId);
        if (count > 0) {
            CacheUtils.remove(CacheConstants.CHANNEL_MENUS);
        }
        return count;
    }

    @Override
    public int updateDisabled(Channel channel) {
        if (channel.getChannelId() < 1) return 0;
        int count = channelMapper.updateDisabled(channel);
        if (count > 0) {
            CacheUtils.remove(CacheConstants.CHANNEL_MENUS);
        }
        return count;
    }

}
